require("dotenv").config({ path: ".env" });

const initDB = require('./src/init/initDB')
const initServer = require('./src/init/initServer')
const initRoute = require('./src/init/initRoute')

const cors = require('cors')
const morgan = require('morgan')
const noMatchMid = require('./src/middleware/404_middleware')
const errorMatchMid = require('./src/middleware/error_middleware')


const express = require("express");
const app = express();

//中间件
app.use(cors({credentials:true,origin:true})) //跨域
app.use(express.json()) // 解析
app.use(morgan('tiny')) // http 请求日志

//静态服务
app.use('/static',express.static('public'))

//初始化路由
initRoute(app)

//404
app.use(noMatchMid) 
//服务器错误统一处理
app.use(errorMatchMid) 
 
const main = async () => {
  //初始化数据库服务
  await initDB();
  //启动 node 服务
  await initServer(app);
};

main();
